package com.breakfast.fun.ui.start;

import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;

import com.breakfast.fun.Constant;
import com.breakfast.fun.MainActivity;
import com.breakfast.fun.R;
import com.breakfast.fun.data.entity.home.CityData;
import com.breakfast.fun.data.entity.user.UserData;
import com.breakfast.fun.ui.home.PermissionActivity;
import com.breakfast.fun.ui.other.city.CityActivity;
import com.breakfast.fun.util.SharedUtil;

public class StartActivity extends PermissionActivity {


    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            CityData cityData = CityData.objectFromData(SharedUtil.getString(StartActivity.this, "city"));
            if (cityData == null) {
                Intent intent = new Intent(StartActivity.this, CityActivity.class);
                startActivity(intent);
                StartActivity.this.finish();
            } else {
                Constant.setCityData(cityData);
                Intent intent = new Intent(StartActivity.this, MainActivity.class);
                startActivity(intent);
                StartActivity.this.finish();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_start);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.setStatusBarColor(getResources().getColor(R.color.main_color));
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {

        initUser();
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Message msg = handler.obtainMessage();
                handler.sendMessage(msg);
            }

        }.start();
    }

    private void initUser() {
        String userData = SharedUtil.getString(this, "user");
        if (userData == null || userData.equals("")) {
            return;
        }
        UserData user = UserData.objectFromData(
                SharedUtil.getString(this, "user"));
        Constant.setUserData(user);

        String userSign = SharedUtil.getString(this, "sign");
        Constant.setUserSign(userSign);
    }


}
